/*******************************************************************************
 * Jimm - Mobile Messaging - J2ME ICQ clone
 * Copyright (C) 2003-05  Jimm Project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *******************************************************************************
 * File: src/jimm/Options.java
 * Version: ###VERSION###  Date: ###DATE###
 * Author(s): Manuel Linsmayer, Andreas Rossbacher, Artyomov Denis, Igor Palkin,
 * Vladimir Kryukov
 ******************************************************************************/
package jimm;

import javax.microedition.lcdui.*;
import jimm.cl.ContactList;
import jimm.comm.*;
import jimm.modules.*;
import jimm.ui.base.*;
import jimm.ui.form.*;
import jimm.ui.menu.*;
import jimm.util.*;
import DrawControls.icons.*; //menu icons

/* Form for editing option values */
public class OptionsForm implements FormListener, SelectListener {
    private MenuModel optionsMenu = new MenuModel();
    private MenuModel colorScheme = null;
    private GraphForm form;
    private int currentOptionsForm;
    
    // Static constants for menu actios
    private static final int OPTIONS_ACCOUNT    = 0;
    private static final int OPTIONS_NETWORK    = 1;
    private static final int OPTIONS_INTERFACE  = 3;
    private static final int OPTIONS_SCHEME     = 4;
    private static final int OPTIONS_HOTKEYS    = 5;
    private static final int OPTIONS_SIGNALING  = 6;
    // #sijapp cond.if modules_TRAFFIC is "true"#
    private static final int OPTIONS_TRAFFIC    = 7;
    // #sijapp cond.end#
    
    private static final int OPTIONS_TIMEZONE   = 8;
    private static final int OPTIONS_ANTISPAM   = 9;
    private static final int OPTIONS_ABSENCE    = 10;
    
    final private String[] hotkeyActionNames = Util.explode(
            "ext_hotkey_action_none"
            + "|" + "info"
            + "|" + "open_chats"
            // #sijapp cond.if modules_HISTORY is "true"#
            + "|" + "history"
            // #sijapp cond.end#
            + "|" + "ext_hotkey_action_onoff"
            + "|" + "keylock"
            // #sijapp cond.if target is "MIDP2" #
            + "|" + "minimize"
            // #sijapp cond.end#
            + "|" + "#sound_off"
            // #sijapp cond.if modules_MAGIC_EYE is "true" #
            + "|" + "magic eye"
            // #sijapp cond.end#
            + "|" + "user_statuses"
            + "|" + "collapse_all_groups",
            '|'
            );
    
    final private int [] hotkeyActions = {
        Options.HOTKEY_NONE,
        Options.HOTKEY_INFO,
        Options.HOTKEY_OPEN_CHATS,
        // #sijapp cond.if modules_HISTORY is "true"#
        Options.HOTKEY_HISTORY,
        // #sijapp cond.end#
        Options.HOTKEY_ONOFF,
        Options.HOTKEY_LOCK,
        // #sijapp cond.if target is "MIDP2" #
        Options.HOTKEY_MINIMIZE,
        // #sijapp cond.end#
        Options.HOTKEY_SOUNDOFF,
        // #sijapp cond.if modules_MAGIC_EYE is "true" #
        Options.HOTKEY_MAGIC_EYE,
        // #sijapp cond.end#
        Options.HOTKEY_STATUSES,
        Options.HOTKEY_COLLAPSE_ALL
    };
    
    public OptionsForm() {
    }
    
    
    // Initialize the kist for the Options menu
    private static final ImageList menuIcons = ImageList.createImageList("/menuicons.png"); //menu icons
    private void initOptionsList() {
        optionsMenu.clean();
        
        optionsMenu.addItem("options_account", menuIcons.iconAt(5), OPTIONS_ACCOUNT); //menu icons
        optionsMenu.addItem("options_network", menuIcons.iconAt(1), OPTIONS_NETWORK); //menu icons
        optionsMenu.addItem("options_interface", menuIcons.iconAt(15), OPTIONS_INTERFACE); //menu icons
        
        if (Scheme.getSchemeNames().length > 2) {
            optionsMenu.addItem("color_scheme", menuIcons.iconAt(16), OPTIONS_SCHEME); //menu icons
        }
        
        if (3 != Options.getInt(Options.OPTION_KEYBOARD)) {
            optionsMenu.addItem("options_hotkeys", menuIcons.iconAt(17), OPTIONS_HOTKEYS); //menu icons
        }
        optionsMenu.addItem("options_signaling", menuIcons.iconAt(8), OPTIONS_SIGNALING); //menu icons
        // #sijapp cond.if modules_ANTISPAM is "true"#
        optionsMenu.addItem("antispam", menuIcons.iconAt(18), OPTIONS_ANTISPAM); //menu icons
        // #sijapp cond.end#
        // #sijapp cond.if modules_ABSENCE is "true" #
        optionsMenu.addItem("absence", menuIcons.iconAt(19), OPTIONS_ABSENCE); //menu icons
        // #sijapp cond.end#
        // #sijapp cond.if modules_TRAFFIC is "true"#
        optionsMenu.addItem("traffic_lng", menuIcons.iconAt(20), OPTIONS_TRAFFIC); //menu icons
        // #sijapp cond.end#
        optionsMenu.addItem("time_zone", menuIcons.iconAt(21), OPTIONS_TIMEZONE); //menu icons
        optionsMenu.setActionListener(this);
        optionsMenu.setDefaultItemCode(currentOptionsForm);
    }
    
    private void addHotKey(String keyName, int option) {
        int optionValue = Options.getInt(option);
        int def = 0;
        for (int i = 0; i < hotkeyActionNames.length; ++i) {
            if (hotkeyActions[i] == optionValue) {
                def = i;
                break;
            }
        }
        form.addSelector(option, keyName, Util.implode(hotkeyActionNames, "|"), def);
    }
    private void saveHotKey(int option) {
        Options.setInt(option, hotkeyActions[form.getSelectorValue(option)]);
    }
    
    private void initHotkeyMenuUI() {
        addHotKey("ext_clhotkey0",     Options.OPTION_EXT_CLKEY0);
        addHotKey("ext_clhotkey4",     Options.OPTION_EXT_CLKEY4);
        addHotKey("ext_clhotkey6",     Options.OPTION_EXT_CLKEY6);
        addHotKey("ext_clhotkeystar",  Options.OPTION_EXT_CLKEYSTAR);
        addHotKey("ext_clhotkeypound", Options.OPTION_EXT_CLKEYPOUND);
        // #sijapp cond.if target is "MIDP2" #
        if (Jimm.isPhone(Jimm.PHONE_SE)) {
            addHotKey("camera", Options.OPTION_EXT_CLKEYCALL);
        } else {
            addHotKey("ext_clhotkeycall", Options.OPTION_EXT_CLKEYCALL);
        }
        // #sijapp cond.end#
    }
    
    ///////////////////////////////////////////////////////////////////////////
    
    /* Activate options menu */
    public void show() {
        initOptionsList();
        new Select(optionsMenu).show();
    }
    
    private void showThemeSelector() {
        String[] strings = Scheme.getSchemeNames();
        colorScheme = null;
        if (strings.length > 1) {
            colorScheme = new MenuModel();
            colorScheme.setActionListener(this);
            for (int i = 0; i < strings.length; ++i) {
                colorScheme.addRawItem(strings[i], null, i);
            }
            colorScheme.setDefaultItemCode(Options.getInt(Options.OPTION_COLOR_SCHEME));
            new Select(colorScheme).show();
        }
    }
    
    private void setChecked(String lngStr, int optValue) {
        form.addCheckBox(optValue, lngStr, Options.getBoolean(optValue));
    }
    
    
    private void createNotifyControls(int modeOpt, String title) {
        // #sijapp cond.if modules_SOUND is "true" #
        form.addCheckBox(modeOpt, title, 0 < Options.getInt(modeOpt));
        // #sijapp cond.end#
    }
    private void saveNotifyControls(int opt) {
        // #sijapp cond.if modules_SOUND is "true" #
        Options.setInt(opt, form.getCheckBoxValue(opt) ? 2 : 0);
        // #sijapp cond.end#
    }
    
    /* Helpers for options UI: */
    private void createSelector(String cap, String items, int opt) {
        form.addSelector(opt, cap, items, Options.getInt(opt));
    }
    private void loadOptionString(int opt, String label, int size, int type) {
        form.addTextField(opt, label, Options.getString(opt), size, type);
    }
    private void saveOptionString(int opt) {
        Options.setString(opt, form.getTextFieldValue(opt));
    }
    private void saveOptionBoolean(int opt) {
        Options.setBoolean(opt, form.getCheckBoxValue(opt));
    }
    private void saveOptionSelector(int opt) {
        Options.setInt(opt, form.getSelectorValue(opt));
    }
    // #sijapp cond.if modules_SOUND is "true" #
    private void loadOptionGauge(int opt, String label) {
        form.addVolumeControl(opt, label, Options.getInt(opt));
    }
    private void saveOptionGauge(int opt) {
        Options.setInt(opt, form.getVolumeValue(opt));
    }
    // #sijapp cond.end#
    // #sijapp cond.if modules_TRAFFIC is "true"#
    private void loadOptionDecimal(int opt, String label) {
        form.addTextField(opt, label, Util.intToDecimal(Options.getInt(opt)),
                6, TextField.ANY);
    }
    private void saveOptionDecimal(int opt) {
        Options.setInt(opt, Util.decimalToInt(form.getTextFieldValue(opt)));
    }
    // #sijapp cond.end#
    private void loadOptionInt(int opt, String label, String variants) {
        String current = String.valueOf(Options.getInt(opt));
        String[] alts = Util.explode(variants, '|');
        int selected = 0;
        String all = "";
        for (int i = 0; i < alts.length; ++i) {
            if (alts[i].equals(current)) {
                selected = i;
            }
            all += "|" + alts[i] + " ";
        }
        form.addSelector(opt, label, all.substring(1), selected);
    }
    private void saveOptionInt(int opt) {
        int val = Util.strToIntDef(form.getSelectorString(opt).trim(), 0);
        Options.setInt(opt, val);
    }

    private void loadOptionInt(int opt, String label, String[] variants, byte[] alts) {
        int current = Options.getInt(opt);
        int selected = 0;
        String all = "";
        for (int i = 0; i < alts.length; ++i) {
            if (alts[i] == current) {
                selected = i;
            }
            all += "|" + variants[i] + " ";
        }
        form.addSelector(opt, label, all.substring(1), selected);
    }
    private void saveOptionInt(int opt, byte[] alts) {
        Options.setInt(opt, alts[form.getSelectorValue(opt)]);
    }
    
    /* Command listener */
    public void formAction(GraphForm form, boolean apply) {
        /* Look for back command */
        if (!apply) {
            back();
            
            // Look for save command
        } else {
            // Save values, depending on selected option menu item
            switch (currentOptionsForm) {
                case OPTIONS_NETWORK:                   
                    saveOptionBoolean(Options.OPTION_AUTO_CONNECT);
                    saveOptionInt(Options.OPTION_CONN_ALIVE_INVTERV);
                    saveOptionInt(Options.OPTION_RECONNECT_NUMBER);
                    initOptionsList();
                    // #sijapp cond.if protocols_ICQ is "true" #
                    // #sijapp cond.if modules_CLIENTS is "true" #
                    byte[] clients = protocol.icq.ClientDetector.instance.getClientsForMask();
                    if (1 < clients.length) {
                        saveOptionInt(Options.OPTIONS_CLIENT, clients);
                        protocol.Protocol[] all = ContactList.getInstance().getProtocols();
                        for (int i = 0; i < all.length; ++i) {
                            if (all[i] instanceof protocol.icq.Icq) {
                                ((protocol.icq.Icq)all[i]).updateClientMask();
                            }
                        }
                    }
                    // #sijapp cond.end#
                    // #sijapp cond.end#
                    // #sijapp cond.if protocols_JABBER is "true" # //add
                    saveOptionString(Options.OPTION_APL_RESOURCE);  //applications resource
                    saveOptionString(Options.OPTION_APL_PRIORITY);  //applications priority
                    // #sijapp cond.end#                            //add 
                    break;
                    
                case OPTIONS_INTERFACE:
                    if (JLocale.langAvailable.length > 1) {
                        int lang = form.getSelectorValue(Options.OPTION_UI_LANGUAGE);
                        Options.setString(Options.OPTION_UI_LANGUAGE, JLocale.langAvailable[lang]);
                    }
                    
                    if (!NativeCanvas.getInstance().hasPointerEvents()) { //add
                    saveOptionBoolean(Options.OPTION_SHOW_SOFTBAR);
                    }                                                     //add
                    // #sijapp cond.if modules_UPDATES is "true" # //updates modules
                    saveOptionBoolean(Options.OPTION_CHECK_UPDATES);
                    // #sijapp cond.end# //updates modules
                    saveOptionBoolean(Options.OPTION_USER_GROUPS);
                    saveOptionBoolean(Options.OPTION_CL_HIDE_OFFLINE);
                    // #sijapp cond.if protocols_JABBER is "true" #     //hide service-contact
                    saveOptionBoolean(Options.OPTION_CL_HIDE_CERVICE);  //hide service-contact
                    // #sijapp cond.end#                                //hide service-contact                   
                    saveOptionBoolean(Options.OPTION_SAVE_TEMP_CONTACT);
//                    saveOptionBoolean(Options.OPTION_SORT_UP_WITH_MSG);   //changed
                    saveOptionBoolean(Options.OPTION_SHOW_STATUS_LINE);
                    
                    saveOptionSelector(Options.OPTION_SORT_UP);         //sort contacts with chat
                    saveOptionSelector(Options.OPTION_CL_SORT_BY);
                    
                    // #sijapp cond.if modules_HISTORY is "true"#
                    saveOptionBoolean(Options.OPTION_HISTORY);
                    saveOptionBoolean(Options.OPTION_SHOW_LAST_MESS); //add
                    // #sijapp cond.end#
                    saveOptionBoolean(Options.OPTION_CLASSIC_CHAT);
                    saveOptionBoolean(Options.OPTION_SWAP_SEND_AND_BACK);
                    saveOptionBoolean(Options.OPTION_TF_FLAGS);
                    saveOptionBoolean(Options.OPTION_UNTITLED_INPUT);
                    saveOptionBoolean(Options.OPTION_DETRANSLITERATE);
                    saveOptionBoolean(Options.OPTION_RECREATE_TEXTBOX);
                    
                    saveOptionBoolean(Options.OPTION_CP1251_HACK);
                    // #sijapp cond.if protocols_ICQ is "true" #
                    saveOptionBoolean(Options.OPTION_DELIVERY_NOTIFICATION);
                    // #sijapp cond.end#
                    
                    // #sijapp cond.if protocols_JABBER is "true" #        //xstatus title in conference
                    // #sijapp cond.if modules_XSTATUSES is "true" #       //xstatus title in conference
                    saveOptionBoolean(Options.OPTION_TITLE_IN_CONFERENCE); //xstatus title in conference
                    // #sijapp cond.end #                                  //xstatus title in conference
                    // #sijapp cond.end #                                  //xstatus title in conference
                    // #sijapp cond.if protocols_JABBER is "true" #          //templates commands
                    saveOptionBoolean(Options.OPTION_COMMANDS_IN_TEMPLATES); //templates commands
                    // #sijapp cond.end#                                     //templates commands
                                        
                    saveOptionSelector(Options.OPTION_FONT_SCHEME);
                    GraphicsEx.setFontScheme(Options.getInt(Options.OPTION_FONT_SCHEME));
                    if (NativeCanvas.getInstance().hasPointerEvents()) { //interface size
                    saveOptionSelector(Options.OPTION_INTERFACE_SCHEME); //interface size
                    }                                                    //interface size

                    saveOptionInt(Options.OPTION_MAX_MSG_COUNT);
                    saveOptionSelector(Options.OPTION_INPUT_MODE);
                    saveOptionSelector(Options.OPTION_KEYBOARD);
                    if (!NativeCanvas.getInstance().hasPointerEvents()) { //key repeat
                    saveOptionString(Options.OPTION_REPEATER_KEY);        //key repeat
                    saveOptionString(Options.OPTION_REPEATER_TIME);       //key repeat
                    }                                                     //key repeat
                    
                    // #sijapp cond.if modules_LIGHT is "true" #
                    if (CustomLight.isSupport()) {
                        saveOptionSelector(Options.OPTION_LIGHT_THEME);
                        CustomLight.switchOn(Options.getInt(Options.OPTION_LIGHT_THEME));
                    }
                    // #sijapp cond.end#
                    
                    final String sysLang = JLocale.getCurrUiLanguage();
                    final String newLang = Options.getString(Options.OPTION_UI_LANGUAGE);
                    if (!sysLang.equals(newLang)) {
                        Options.setBoolean(Options.OPTIONS_LANG_CHANGED, true);
                    }
                    ContactList.getInstance().getManager().update();
                    break;

                case OPTIONS_HOTKEYS:
                    saveHotKey(Options.OPTION_EXT_CLKEY0);
                    saveHotKey(Options.OPTION_EXT_CLKEY4);
                    saveHotKey(Options.OPTION_EXT_CLKEY6);
                    saveHotKey(Options.OPTION_EXT_CLKEYSTAR);
                    saveHotKey(Options.OPTION_EXT_CLKEYPOUND);
                    // #sijapp cond.if target is "MIDP2" #
                    saveHotKey(Options.OPTION_EXT_CLKEYCALL);
                    // #sijapp cond.end#
                    break;
                    
                case OPTIONS_SIGNALING:
                    // #sijapp cond.if modules_SOUND is "true" #
                    saveOptionGauge(Options.OPTION_NOTIFY_VOLUME);
                    // #sijapp cond.end#
                    saveOptionSelector(Options.OPTION_VIBRATOR);
                    saveOptionString(Options.OPTION_VIBRATOR_TIME);        //vibra time
                    // #sijapp cond.if modules_SOUND is "true" #
                    saveNotifyControls(Options.OPTION_ONLINE_NOTIF_MODE);
                    saveNotifyControls(Options.OPTION_OFFLINE_NOTIF_MODE); //offline sound
                    saveNotifyControls(Options.OPTION_MESS_NOTIF_MODE);
                    // #sijapp cond.if protocols_JABBER is "true" #        //other sound
                    saveNotifyControls(Options.OPTION_OTHER_NOTIF_MODE);   //other sound
                    // #sijapp cond.end#                                   //other sound
                    saveNotifyControls(Options.OPTION_TYPING_MODES);       //typing
                    // #sijapp cond.end#
                    saveOptionBoolean(Options.OPTION_NOTIFY_IN_AWAY);
                    saveOptionBoolean(Options.OPTION_ALARM);
                    // #sijapp cond.if protocols_MRIM is "true" | protocols_JABBER is "true" #  //add
                    saveOptionBoolean(Options.OPTION_BLOG_NOTIFY);
                    // #sijapp cond.end #                                                       //add
                    saveOptionBoolean(Options.OPTION_EYE_NOTIFY);          //eye sound
                    saveOptionSelector(Options.OPTION_TYPING_MODE);

                    saveOptionBoolean(Options.OPTION_POPUP_OVER_SYSTEM);
                    saveOptionBoolean(Options.OPTION_TICKER_SYSTEM);       //ticker
                    // #sijapp cond.if target="MIDP2"#
                    saveOptionBoolean(Options.OPTION_BRING_UP);
                    // #sijapp cond.end#
                    break;
                    
                    // #sijapp cond.if modules_ANTISPAM is "true"#
                case OPTIONS_ANTISPAM:
                    saveOptionString(Options.OPTION_ANTISPAM_MSG);
                    saveOptionString(Options.OPTION_ANTISPAM_ANSWER);
                    saveOptionString(Options.OPTION_ANTISPAM_HELLO);
                    saveOptionString(Options.OPTION_ANTISPAM_KEYWORDS);
                    saveOptionBoolean(Options.OPTION_ANTISPAM_ENABLE);
                    break;
                    // #sijapp cond.end#
                    
                    // #sijapp cond.if modules_ABSENCE is "true" #
                case OPTIONS_ABSENCE:
                    saveOptionBoolean(Options.OPTION_AA_BLOCK);
                    Options.setInt(Options.OPTION_AA_TIME, form.getSelectorValue(Options.OPTION_AA_TIME) * 5);
                    saveOptionBoolean(Options.OPTION_AA_LOCK); //autoblock
                    jimm.modules.AutoAbsence.instance.updateOptions();
                    break;
                    // #sijapp cond.end#
                    
                    // #sijapp cond.if modules_TRAFFIC is "true"#
                case OPTIONS_TRAFFIC:
                    saveOptionDecimal(Options.OPTION_COST_OF_1M);
                    Options.setInt(Options.OPTION_COST_PACKET_LENGTH,
                            Util.strToIntDef(form.getTextFieldValue(Options.OPTION_COST_PACKET_LENGTH), 0) * 1024);
                    saveOptionString(Options.OPTION_CURRENCY);
                    break;
                    // #sijapp cond.end#
                    
                case OPTIONS_TIMEZONE: {
                    /* Set up time zone*/
                    int timeZone = form.getSelectorValue(Options.OPTIONS_GMT_OFFSET) - 12;
                    Options.setInt(Options.OPTIONS_GMT_OFFSET, timeZone);
                    
                    /* Translate selected time to GMT */
                    int[] currDateTime = Util.createDate(Util.createCurrentDate(false));
                    int hour = currDateTime[Util.TIME_HOUR];
                    int selHour = form.getSelectorValue(Options.OPTIONS_LOCAL_OFFSET) - timeZone;
                    selHour = selHour - 12;
                    
                    /* Calculate diff. between selected GMT time and phone time */
                    int localOffset = (selHour + 12 + 24) % 24 - 12;
                    Options.setInt(Options.OPTIONS_LOCAL_OFFSET, localOffset);
                    break;
                }
                
            }
            
            /* Save options */
            Options.safeSave();
            back();
        }
        
    }
    
    public void select(Select select, MenuModel model, int cmd) {
        if (colorScheme == model) {
            final int currentColorScheme = cmd;
            Scheme.setColorScheme(currentColorScheme);
            Options.safeSave();
            select.back();
            colorScheme = null;
            return;
        }
        
        // Add elements, depending on selected option menu item
        currentOptionsForm = cmd;
        // Delete all items
        form = new GraphForm("options_lng", "save", "back", this);
        form.setCaption(model.getItemText(currentOptionsForm));
        switch (currentOptionsForm) {
            case OPTIONS_ACCOUNT:
                new AccountsForm().show();
                return;
                
            case OPTIONS_NETWORK:
                form.addString("nt_connect", null);                                                  //add
                setChecked("auto_connect", Options.OPTION_AUTO_CONNECT);                
                loadOptionInt(Options.OPTION_CONN_ALIVE_INVTERV, "timeout_interv", "60|120|240|300|600|999");
                loadOptionInt(Options.OPTION_RECONNECT_NUMBER, "reconnect_number", "1|10|50|100");
                // #sijapp cond.if protocols_ICQ is "true" #
                // #sijapp cond.if modules_CLIENTS is "true" #
                byte[] clients = protocol.icq.ClientDetector.instance.getClientsForMask();
                if (1 < clients.length) {
                    String[] alts = new String[clients.length];
                    for (int i = 0; i < clients.length; ++i) {
                        alts[i] = protocol.icq.ClientDetector.instance.getClient(clients[i]);
                    }
                    loadOptionInt(Options.OPTIONS_CLIENT, "icq-client", alts, clients);
                }
                // #sijapp cond.end#
                // #sijapp cond.end#
                // #sijapp cond.if protocols_JABBER is "true" #                                      //add
                form.addString("nt_server", null);                                                   //add
                loadOptionString(Options.OPTION_APL_RESOURCE, "apl_resource", 16, TextField.ANY);    //applications resource
                loadOptionString(Options.OPTION_APL_PRIORITY, "apl_priority", 3, TextField.NUMERIC); //applications priority
                // #sijapp cond.end#                                                                 //add
                break;
                
            case OPTIONS_INTERFACE:
                // Initialize elements (interface section)
                if (JLocale.langAvailable.length > 1) {
                    String items = "";
                    int cur = 0;
                    for (int j = 0; j < JLocale.langAvailable.length; ++j) {
                        boolean selected = JLocale.langAvailable[j].equals(Options.getString(Options.OPTION_UI_LANGUAGE));
                        items += "|" + JLocale.langAvailableName[j];
                        if (selected) {
                            cur = j;
                        }
                    }
                    items = items.substring(1);
                    form.addSelector(Options.OPTION_UI_LANGUAGE, "language", items, cur);
                }
                
                // #sijapp cond.if modules_UPDATES is "true" # //updates modules
                setChecked("check_updates", Options.OPTION_CHECK_UPDATES);               
                // #sijapp cond.end# //updates modules
                form.addString("contact_list", null);
                setChecked("show_user_groups", Options.OPTION_USER_GROUPS);
                setChecked("hide_offline", Options.OPTION_CL_HIDE_OFFLINE);
                // #sijapp cond.if protocols_JABBER is "true" #                     //hide service-contact
                setChecked("hide_offline_service", Options.OPTION_CL_HIDE_CERVICE); //hide service-contact
                // #sijapp cond.end#                                                //hide service-contact
                setChecked("save_temp_contacts",   Options.OPTION_SAVE_TEMP_CONTACT);
//                setChecked("contacts_with_msg_at_top", Options.OPTION_SORT_UP_WITH_MSG); //changed
                setChecked("show_status_line", Options.OPTION_SHOW_STATUS_LINE);
                // #sijapp cond.if protocols_JABBER is "true" #                         //xstatus title in conference
                // #sijapp cond.if modules_XSTATUSES is "true" #                        //xstatus title in conference
                setChecked("title_in_conference",  Options.OPTION_TITLE_IN_CONFERENCE); //xstatus title in conference
                // #sijapp cond.end #                                                   //xstatus title in conference
                // #sijapp cond.end #                                                   //xstatus title in conference 
                createSelector("contacts_at_top",                                       //sort contacts with chat
                        "no" + "|" + "with_messages" + "|" + "with_chat",               //sort contacts with chat
                        Options.OPTION_SORT_UP);                                        //sort contacts with chat
                createSelector("sort_by",
                        "sort_by_status" + "|" + "sort_by_online" + "|" + "sort_by_name",
                        Options.OPTION_CL_SORT_BY);
                
                
                form.addString("chat", null);   
                // #sijapp cond.if modules_HISTORY is "true"#
                setChecked("use_history",     Options.OPTION_HISTORY);
                setChecked("show_prev_mess",  Options.OPTION_SHOW_LAST_MESS); //add
                // #sijapp cond.end#
                setChecked("cl_chat",         Options.OPTION_CLASSIC_CHAT);
                loadOptionInt(Options.OPTION_MAX_MSG_COUNT, "max_message_count", "10|50|100|250|500|1000");
                
                form.addString("interface", null);
                if (!NativeCanvas.getInstance().hasPointerEvents()) {                                  //add
                setChecked("show_softbar", Options.OPTION_SHOW_SOFTBAR);                               //changed
                loadOptionString(Options.OPTION_REPEATER_KEY, "repeater_key", 3, TextField.NUMERIC);   //key repeat
                loadOptionString(Options.OPTION_REPEATER_TIME, "repeater_time", 3, TextField.NUMERIC); //key repeat
                }                                                                                      //add
                createSelector("fonts",
                        "fonts_smallest" + "|" + "fonts_small" + "|" + "fonts_normal" + "|" + "fonts_large" + "|" + "fonts_largest",
                        Options.OPTION_FONT_SCHEME);
                if (NativeCanvas.getInstance().hasPointerEvents()) {                            //interface size
                createSelector("interface_size",                                                //interface size
                        "interface_small" + "|" + "interface_normal" + "|" + "interface_large", //interface size
                        Options.OPTION_INTERFACE_SCHEME);                                       //interface size
                }                                                                               //interface size

                form.addString("textbox", null);
                // #sijapp cond.if protocols_JABBER is "true" #                            //templates commands
                setChecked("commands_in_templates", Options.OPTION_COMMANDS_IN_TEMPLATES); //templates commands
                // #sijapp cond.end#                                                       //templates commands              
                setChecked("swap_send_and_back", Options.OPTION_SWAP_SEND_AND_BACK);
                setChecked("auto_case",       Options.OPTION_TF_FLAGS);
                setChecked("untitled_input",  Options.OPTION_UNTITLED_INPUT);
                setChecked("recreate_textbox", Options.OPTION_RECREATE_TEXTBOX);
                setChecked("detransliterate", Options.OPTION_DETRANSLITERATE);
                createSelector("input_mode",
                        "default" + "|" + "latin" + "|" + "cyrillic",
                        Options.OPTION_INPUT_MODE);
                createSelector("keyboard_type",
                        "default" + "|" + "QWERTY" + "|" + "old_se_keys" + "|" + "no",
                        Options.OPTION_KEYBOARD);

                form.addString("messages", null);
                setChecked("cp1251",          Options.OPTION_CP1251_HACK);
                // #sijapp cond.if protocols_ICQ is "true" #
                setChecked("delivery_notification", Options.OPTION_DELIVERY_NOTIFICATION);
                // #sijapp cond.end #
                
                // #sijapp cond.if modules_LIGHT is "true" #
                if (CustomLight.isSupport()) {
                    createSelector("light_theme",
                            "off" + "|" + "light_min" + "|" + "light_middle"
                            + "|" + "light_max"  + "|" + "light_message_only",
                            Options.OPTION_LIGHT_THEME);
                }
                // #sijapp cond.end#
                break;
                
            case OPTIONS_SCHEME:
                showThemeSelector();
                return;
                
            case OPTIONS_HOTKEYS:
                initHotkeyMenuUI();
                break;
                
                /* Initialize elements (Signaling section) */
            case OPTIONS_SIGNALING:
                /* Vibrator notification controls */
                
                // #sijapp cond.if modules_SOUND is "true" #
                if (Notify.getSound().hasAnySound()) {
                    loadOptionGauge(Options.OPTION_NOTIFY_VOLUME, "volume");
                }
                setChecked("notify_in_away", Options.OPTION_NOTIFY_IN_AWAY); //changed
                // #sijapp cond.end#
                createSelector(
                        "vibration",
                        "no" + "|" + "yes" + "|" + "when_locked",
                        Options.OPTION_VIBRATOR);
                loadOptionString(Options.OPTION_VIBRATOR_TIME, "vibration_time", 4, TextField.NUMERIC); //vibra time
                
                // #sijapp cond.if modules_SOUND is "true" #
                createNotifyControls(Options.OPTION_MESS_NOTIF_MODE,
                        "message_notification");
                // #sijapp cond.if protocols_JABBER is "true" #          //other sound
                createNotifyControls(Options.OPTION_OTHER_NOTIF_MODE,    //other sound
                        "other_notification");                           //other sound
                // #sijapp cond.end#                                     //other sound
                createNotifyControls(Options.OPTION_ONLINE_NOTIF_MODE,
                        "onl_notification");
                createNotifyControls(Options.OPTION_OFFLINE_NOTIF_MODE,  //offline sound
                        "off_notification");                             //offline sound
                setChecked("alarm", Options.OPTION_ALARM);
                // #sijapp cond.if protocols_MRIM is "true" | protocols_JABBER is "true" #  //add
                setChecked("blog_notify", Options.OPTION_BLOG_NOTIFY);
                // #sijapp cond.end #                                                       //add
                setChecked("eye_notify", Options.OPTION_EYE_NOTIFY);     //eye sound                
                // #sijapp cond.end#
                
                /* Typing notification controls */
                // #sijapp cond.if modules_SOUND is "true" #      //typing
                createNotifyControls(Options.OPTION_TYPING_MODES, //typing
                        "typing_notify");                         //typing
                // #sijapp cond.end#                              //typing                
                createSelector("typing_notify_mode",              //changed
                        "no" + "|" + "typing_incoming" + "|" + "typing_both",
                        Options.OPTION_TYPING_MODE);
                
                setChecked("popup_win_over_system", Options.OPTION_POPUP_OVER_SYSTEM);                
                // #sijapp cond.if target="MIDP2"#
                /* Midlet auto bring up controls on MIDP2 */
                setChecked("bring_up", Options.OPTION_BRING_UP);
                // #sijapp cond.end#
                setChecked("ticker_system", Options.OPTION_TICKER_SYSTEM); //ticker
                break;
                
                // #sijapp cond.if modules_ANTISPAM is "true"#
            case OPTIONS_ANTISPAM:
                setChecked("on", Options.OPTION_ANTISPAM_ENABLE);
                loadOptionString(Options.OPTION_ANTISPAM_MSG, "antispam_msg", 256, TextField.ANY);
                loadOptionString(Options.OPTION_ANTISPAM_ANSWER, "antispam_answer", 256, TextField.ANY);
                loadOptionString(Options.OPTION_ANTISPAM_HELLO, "antispam_hello", 256, TextField.ANY);
                loadOptionString(Options.OPTION_ANTISPAM_KEYWORDS, "antispam_keywords", 512, TextField.ANY);
                break;
                // #sijapp cond.end#
                
                // #sijapp cond.if modules_ABSENCE is "true" #
            case OPTIONS_ABSENCE:
                setChecked("after_block", Options.OPTION_AA_BLOCK);
                form.addSelector(Options.OPTION_AA_TIME, "after_time", "off"+"|5 |10 |15 ", Options.getInt(Options.OPTION_AA_TIME) / 5);
                setChecked("autoblock", Options.OPTION_AA_LOCK); //autoblock
                //form.addChoiceGroup(Options.OPTION_AUTOABSENCE, null, Choice.MULTIPLE);
                //setChecked(Options.OPTION_AUTOABSENCE, "autoanswer", Options.OPTION_AUTOABSENCE);
                //loadOptionString(Options.OPTION_AUTOANSWER, "answer", 256, TextField.ANY);
                break;
                // #sijapp cond.end#
                
                /* Initialize elements (cost section) */
                // #sijapp cond.if modules_TRAFFIC is "true"#
            case OPTIONS_TRAFFIC:
                loadOptionDecimal(Options.OPTION_COST_OF_1M, "cp1m");
                form.addTextField(Options.OPTION_COST_PACKET_LENGTH,
                        "plength",
                        String.valueOf(Options.getInt(Options.OPTION_COST_PACKET_LENGTH) / 1024),
                        4, TextField.NUMERIC);
                loadOptionString(Options.OPTION_CURRENCY, "currency", 4, TextField.ANY);
                break;
                // #sijapp cond.end#
                
            case OPTIONS_TIMEZONE: {
                String items = "";
                int gmtOffset = Options.getInt(Options.OPTIONS_GMT_OFFSET);
                
                for (int i = -12; i <= 13; ++i) {
                    items += "|" + "GMT" + (i < 0 ? "" : "+") + i + ":00";
                }
                items = items.substring(1);
                form.addSelector(Options.OPTIONS_GMT_OFFSET, "time_zone", items, gmtOffset + 12);
                
                items = "";
                int cur = 0;

                int[] currDateTime = Util.createDate(Util.createCurrentDate(false));
                int minutes = currDateTime[Util.TIME_MINUTE];
                int hour = currDateTime[Util.TIME_HOUR];
                int startHour = hour - Options.getInt(Options.OPTIONS_LOCAL_OFFSET)
                        - Options.getInt(Options.OPTIONS_GMT_OFFSET) - 12;
                for (int i = startHour; i < (startHour + 24); ++i) {
                    items += "|" + ((i + 24) % 24) + (minutes < 10 ? ":0" : ":") + minutes;
                    if (hour == i) {
                        cur = i - startHour;
                    }
                }
                items = items.substring(1);
                form.addSelector(Options.OPTIONS_LOCAL_OFFSET, "local_time", items, cur);
                break;
            }
        }
        form.show();
    }
    private void back() {
        form.back();
    }
}